package cn.luoshaocheng.pet.controller;

import cn.luoshaocheng.basic.exception.BusinessException;
import cn.luoshaocheng.basic.util.LoginContext;
import cn.luoshaocheng.pet.dto.AcceptDto;
import cn.luoshaocheng.pet.service.ISearchMasterMsgService;
import cn.luoshaocheng.pet.domain.SearchMasterMsg;
import cn.luoshaocheng.pet.query.SearchMasterMsgQuery;
import cn.luoshaocheng.basic.util.jsonResult;
import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return jsonResult 响应给前端
     */
    @PutMapping
    public jsonResult addAndUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return jsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return jsonResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public jsonResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return jsonResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return jsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return jsonResult 响应给前端
    */
    @PatchMapping
    public jsonResult patchDel(@RequestBody List<Long> ids){
        try {
                searchMasterMsgService.patchDel(ids);
            return jsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg loadById(@PathVariable("id")Long id) {
        return searchMasterMsgService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> loadAll(){
        return searchMasterMsgService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }
    @PostMapping("/publish")
    public jsonResult  publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request) {
        try {
            // 获取当前登录人信息
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.publish(smm,logininfo.getId());
            return jsonResult.me();
        } catch (Exception e) {
            return jsonResult.me().setMsg("网络异常");
        }
    }

    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        try {
            // 获取当前登录人信息
            Logininfo logininfo = LoginContext.getLogininfo(request);
           return  searchMasterMsgService.toHandle(query,logininfo.getId());

        } catch (Exception e) {
            return new  PageList<SearchMasterMsg>();
        }
    }

    @GetMapping("/reject/{id}")
    public jsonResult reject(@PathVariable("id")Long id) {
        try {
            searchMasterMsgService.reject(id);
            return  jsonResult.me();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    @PostMapping("/accept")
    public jsonResult accept(@RequestBody AcceptDto dto,HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.accept(dto,logininfo.getId());
            return jsonResult.me();
        } catch (Exception e) {
          e.printStackTrace();
            return jsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }
}
